package com.example.pointxytest.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.pointxytest.R;
import com.example.pointxytest.widget.OnDialogClick;

/**
 * FileName: InputDialog
 * Author: huangyuguang
 * Date: 2022/5/7
 * Description: 输入框弹窗
 */
public class InputDialog extends Dialog implements View.OnClickListener {
    private String hintStr;
    private String content;
    private EditText etInput;

    private OnDialogClick onDialogClick;

    public InputDialog(@NonNull Context context,String hintStr,String content) {
        super(context);
        this.hintStr = hintStr;
        this.content = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_input);
        etInput = findViewById(R.id.et_input);
        if(!TextUtils.isEmpty(hintStr)){
            etInput.setHint(hintStr);
        }
        etInput.setText(content);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_ok).setOnClickListener(this);

        KeyboardUtils.showSoftInput(etInput);
    }

    public void setOnDialogClick(OnDialogClick onDialogClick) {
        this.onDialogClick = onDialogClick;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.btn_cancel:
            case R.id.btn_ok:
                if(onDialogClick != null){
                    String email = etInput.getText().toString().trim();
                    if(RegexUtils.isEmail(email)){
                        dismiss();
                        onDialogClick.result(0,email);
                    }else {
                        ToastUtils.showLong("请输入正确的邮箱地址");
                    }
                }
                break;
        }
    }

    @Override
    public void dismiss() {
        KeyboardUtils.hideSoftInput(etInput);
        super.dismiss();
    }
}
